﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Client
{
    public partial class TaskbarForm : Form
    {
        private string titleText;
        private string contentText;
        private Rectangle WorkAreaRectangle;
        private int currentTop;
        private int CurrentState;
        private Rectangle TitleRectangle;
        private Rectangle TitlebarRectangle;
        private Rectangle ContentRectangle;
        private Rectangle CloseBtnRectangle;
        private Bitmap BackgroundBitmap;

        public TaskbarForm()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        //发送消息//winuser.h 中有函数原型定义
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture(); //释放鼠标捕捉winuser.h
        [DllImport("user32.dll")] //winuser.h
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        public void ShowForm(string ftitletext, string fcontenttext, Rectangle fRegionofFormTitle, Rectangle fRegionofFormTitlebar, Rectangle fRegionofFormContent, Rectangle fRegionofCloseBtn)
        {
            titleText = ftitletext;
            contentText = fcontenttext;
            WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
            this.Top = WorkAreaRectangle.Height + this.Height;
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Normal;
            this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
            CurrentState = 1;
            timer1.Enabled = true;
            TitleRectangle = fRegionofFormTitle;
            TitlebarRectangle = fRegionofFormTitlebar;
            ContentRectangle = fRegionofFormContent;
            CloseBtnRectangle = fRegionofCloseBtn;
            ShowWindow(this.Handle, 4); //#define SW_SHOWNOACTIVATE 4
        }

        public void SetBackgroundBitmap(Image image, Color transparencyColor)
        {
            BackgroundBitmap = new Bitmap(image);
            Width = BackgroundBitmap.Width;
            Height = BackgroundBitmap.Height;
            Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
        }

        private Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
        {
            if (bitmap == null)
                throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");

            int height = bitmap.Height;
            int width = bitmap.Width;
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            for (int j = 0; j < height; j++)
                for (int i = 0; i < width; i++)
                {
                    if (bitmap.GetPixel(i, j) == transparencyColor)
                        continue;
                    int x0 = i;
                    while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))
                        i++;
                    path.AddRectangle(new Rectangle(x0, j, i - x0, 1));
                }
            Region region = new Region(path);
            path.Dispose();
            return region;
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Graphics grfx = e.Graphics;
            grfx.PageUnit = GraphicsUnit.Pixel;
            Graphics offScreenGraphics;
            Bitmap offscreenBitmap;
            offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);
            offScreenGraphics = Graphics.FromImage(offscreenBitmap);
            if (BackgroundBitmap != null)
            {
                offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height);
            }
            DrawText(offScreenGraphics);
            grfx.DrawImage(offscreenBitmap, 0, 0);
        }

        private void DrawText(Graphics grfx)
        {
            grfx.DrawString(titleText, DefaultFont, Brushes.Black, TitleRectangle);
            grfx.DrawString(contentText, DefaultFont, Brushes.Black, ContentRectangle);
        }

        private const int WM_NCLBUTTONDOWN = 0x00A1;
        private const int HT_CAPTION = 0x002;

        private void TaskbarForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动
                {
                    ReleaseCapture(); //释放鼠标捕捉
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的消息至该窗体(标题栏)
                }
                if (CloseBtnRectangle.Contains(e.Location)) //单击Close按钮关闭 
                {
                    this.Hide();
                    currentTop = 1;
                }
                if (ContentRectangle.Contains(e.Location)) //单击内容区域
                {
                    this.Hide();
                    currentTop = 1;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (currentTop < this.Top && currentTop > 1)
            {
                if (CurrentState == 1)
                    currentTop++;
                else if (CurrentState == 2)
                    currentTop--;
                this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
            }
            else
            {
                timer1.Enabled = false;
                if (CurrentState == 1)
                {
                    timer2.Enabled = true;
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            CurrentState = 2;
            timer1.Enabled = true;
        }
    }
}
